package qeb.handheldmedical.exam;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import qeb.handheldmedical.R;
import qeb.handheldmedical.adapter.AppointmentStudyListAdapter;
import qeb.handheldmedical.adapter.SlideListView;
import qeb.handheldmedical.dialog.MultiDialog;
import qeb.handheldmedical.helper.WSHelper;
import qeb.handheldmedical.model.User;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

/**
 * @author 作者(dugaoyi) E-mail:dugaoyi@163.com
 * @version 创建时间：2e016年8月31日 下午7:13:49
 * @declaration 学习预约
 */
public class AppointmentStudyList extends Activity {
	User usr = new User();
	private SlideListView slvStudy;
	private TextView tv_top_title;
	Button btn_title_back, btn_title_right;
	private JSONArray jsonArray;
	private AppointmentStudyListAdapter adapter;
	private MultiDialog mDialog;
	private String stritemId;// 预约课程ID

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.exam_appointment_study_list);
		usr = (User) getIntent().getSerializableExtra("userinfo");
		slvStudy = (SlideListView) findViewById(R.id.slvStudy);
		tv_top_title = (TextView) findViewById(R.id.tv_top_title);
		tv_top_title.setText("学习预约");
		btn_title_back = (Button) findViewById(R.id.btn_title_back);
		btn_title_back.setOnClickListener(MyOnClickListener);
		btn_title_right = (Button) findViewById(R.id.btn_title_right);
		btn_title_right.setOnClickListener(MyOnClickListener);
		getData();
		dataBind();
	}

	/**
	 * 绑定
	 */
	private void dataBind() {
		// TODO Auto-generated method stub
		if (jsonArray != null && jsonArray.length() > 0) {

			if (adapter != null) {
				adapter.notifyDataSetChanged();
			}

			adapter = new AppointmentStudyListAdapter(jsonArray.length(),
					AppointmentStudyList.this, jsonArray);

			slvStudy.setAdapter(adapter);

			slvStudy.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					JSONObject jObj;
					try {
						jObj = jsonArray.getJSONObject(position);
						mDialog = new MultiDialog(AppointmentStudyList.this,
								MultiDialog.DialogType.CONFIRM);
						mDialog.setTitle("系统提醒");
						// 课程报名标志
						String strisbm = jObj.getString("ISBM");
						String strMessage = "";
						stritemId = jObj.getString("ITEMID");
						if (strisbm.indexOf("已报名") > 0) {
							strMessage = "您确定要取消【"
									+ jObj.getString("LEARNNAME") + "】！";
						} else {
							strMessage = "您确定要预约【"
									+ jObj.getString("LEARNNAME") + "】！";
						}
						mDialog.setMessage(strMessage);
						mDialog.setDialogGoListener(new OnClickListener() {

							@Override
							public void onClick(View v) {
								// TODO Auto-generated method stub
								mDialog.cancel();
								Map<String, String> map = new HashMap<String, String>();
								map.put("userID", usr.GH);
								map.put("itemID", stritemId);
								JSONArray resultJson = WSHelper
										.getWebserviceFun("SetLearn", map);
								JSONObject xt = null;
								try {
									xt = resultJson.getJSONObject(0);
									String pda_error_message = xt
											.getString("pda_error_message");
									String pda_error_state = xt
											.getString("pda_error_state");

									Toast tt = Toast.makeText(
											AppointmentStudyList.this,
											pda_error_message,
											Toast.LENGTH_LONG);
									tt.setGravity(Gravity.CENTER, 0, 0);
									tt.show();
									if (pda_error_state.equals("0")) {
										getData();
										dataBind();
									}
								} catch (JSONException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}

							}
						});
						mDialog.show();
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}

			});
		}
	}

	/**
	 * 读取数据
	 */
	private void getData() {
		// TODO Auto-generated method stub
		if (usr.BQID != null) {

			Map<String, String> map = new HashMap<String, String>();
			map.put("userID", usr.GH);
			jsonArray = WSHelper.getWebserviceFun("GetLearningReservationItem",
					map);

		}
	}

	private OnClickListener MyOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_title_back:// 返回按钮
				AppointmentStudyList.this.finish();
				break;
			}
		}
	};

}
